package com.easy.gao.imageloadertest;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Gao on .
 * 作者 ：高建勋
 * 创建时间 2018-10-22
 */

public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ViewHolder> {
    List<String> data;
    Context context;
    ImageListener imageListener;
    public void setImageListener(ImageListener imageListener) {
        this.imageListener = imageListener;
    }
    public ImageAdapter(Context context, List<String> data) {
        this.data = data;
        this.context = context;
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.image_adapter,
                parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        RequestOptions options=new RequestOptions();
        options.error(R.mipmap.ic_launcher);
        Glide.with(context).load(data.get(position)).apply(options).into(holder.img);
        holder.img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                imageListener.imageLister(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img)
        ImageView img;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    interface ImageListener {
        void imageLister(int posituon);
    }
}
